#ifndef _WIDGETTREE_H_
#define _WIDGETTREE_H_

#include <QtGui/QTreeWidget>
#include <QtGui/QProgressBar>

#include <QDir>
#include <QString>
#include <QThread>

#include "PropExtractor.h"

class WidgetTree;

enum DataType
{
	eDataType_Unknown,
	eDataType_Folder,
	eDataType_Image,
	eDataType_Model,
	eDataType_Animation
};

struct TreeItemData
{
	QString text;
	QString workingdir;

	pe::PEImageRaw* image;
	pe::PEArchiveRaw* archive;
	unsigned int index;

	DataType type;
};

class TreeItem : public QTreeWidgetItem
{

public:

	TreeItem(TreeItemData* a_Data);
	TreeItem(QString a_Folder, DataType a_Type = eDataType_Folder);
	TreeItem(QString a_Path, QString a_WorkingDir, DataType a_Type = eDataType_Unknown);
	TreeItem(QString a_Text, pe::PEArchiveRaw* a_ZipHandle, unsigned int a_Index, DataType a_Type = eDataType_Unknown);
	~TreeItem();

	pe::PEImageRaw* GetImageHandle();
	bool GetFileHandle(pe::PEStream* a_Target);
	bool SaveToFile(QString a_Path);

	TreeItemData* GetData() { return m_Data; }
	DataType GetType() { return m_Data->type; }
	QString GetText() { return m_Data->text; }
	QString GetWorkingDir() { return m_Data->workingdir; }

private:	

	void SelectType();

	TreeItemData* m_Data;

};

class TreeThread : public QThread
{

	Q_OBJECT

public:

	TreeThread();
	~TreeThread();

	void run();

	void SetArchive(pe::PEArchiveRaw* a_Archive);
	QList<TreeItem*>& GetList();
	void Clear();

signals:

	void UpdateBar();

private:

	void LoadArchiveEntries();

	QList<TreeItem*> m_TreeList;
	pe::PEArchiveRaw* m_Archive;

};

class WidgetTree : public QTreeWidget
{

	Q_OBJECT

public:

	WidgetTree(QWidget* a_Parent = 0);
	~WidgetTree();

	void LoadFromDir(QString a_Path, bool a_Relative = true);
	void LoadFromZip(QString a_ZipPath, bool a_Relative = true);

	bool GetSelectedImage(pe::PEImageRaw& a_Target);

public slots:

	void LoadFinished();
	void ItemLoaded();

	void SaveFile();
	void SaveFileUncompressed();

	void ShowContextMenu(const QPoint& a_Pos);
 
private:

	void dragEnterEvent(QDragEnterEvent* a_Event);
	void dragMoveEvent(QDragMoveEvent* a_Event);
	void dragLeaveEvent(QDragLeaveEvent* a_Event);
	void dropEvent(QDropEvent* a_Event);

	void contextMenuEvent(QContextMenuEvent* a_Event);
	//void customContextMenuRequested(const QPoint& a_Pos);

	QList<QAction*> m_ActionDir;
	QList<QAction*> m_ActionFile;

	int m_ItemCurrent;
	bool m_Zip;
	QDir m_FileSystem;
	TreeThread* m_Thread;
	QProgressBar* m_Bar;
	//pe::AliceArchiveRed* m_ArchiveRed;
	//pe::AliceArchiveAfa* m_ArchiveAfa;
	//pe::AliceArchiveAld* m_ArchiveAld;
	pe::PEArchiveRaw* m_Archive;

}; // class WidgetTree

#endif